package com.gc.ykqx.service.util;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.gc.ykqx.R;
import com.gc.ykqx.service.entity.Count;
import com.gc.ykqx.service.entity.Site;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WSD on 2017/4/18.
 */

public class ParseUtil {
    private static final String TAG = "ParseUtil";
    private static int[] yjImg = {R.mipmap.ic_launcher, R.mipmap.yj_snow_orange, R.mipmap.yj_snow_red, R.mipmap.yj_snow_yellow, R.mipmap.yj_snow_blue,
            R.mipmap.yj_rain_orange, R.mipmap.yj_rain_red, R.mipmap.yj_rain_yellow, R.mipmap.yj_rain_blue,
            R.mipmap.yj_hail_orange, R.mipmap.yj_hail_red,
            R.mipmap.yj_wind_orange, R.mipmap.yj_wind_red, R.mipmap.yj_wind_yellow,
            R.mipmap.yj_fog_orange, R.mipmap.yj_fog_red, R.mipmap.yj_fog_yellow,
            R.mipmap.yj_road_orange, R.mipmap.yj_road_red, R.mipmap.yj_road_yellow,
            R.mipmap.yj_dry_orange, R.mipmap.yj_dry_red,
            R.mipmap.yj_tem_orange, R.mipmap.yj_tem_red,
            R.mipmap.yj_cold_orange, R.mipmap.yj_cold_re, R.mipmap.yj_cold_yellow, R.mipmap.yj_cold_blue,
            R.mipmap.yj_thunder_orange, R.mipmap.yj_thunder_red, R.mipmap.yj_thunder_yellow,
            R.mipmap.yj_haze_orange, R.mipmap.yj_haze_yellow,
            R.mipmap.yj_frost_orange, R.mipmap.yj_frost_yellow, R.mipmap.yj_frost_blue,
            R.mipmap.yj_tpy_orange, R.mipmap.yj_tpy_red, R.mipmap.yj_tpy_yellow, R.mipmap.yj_tpy_blue};
    private static int[] yjColor = {R.color.colorWhite, R.color.colorOrange, R.color.colorRed, R.color.colorYellow, R.color.colorBlue};

    /*
    check is load weather by time
    if(loseNum>3) return false
     */
    public static boolean getWea(ArrayList<Site> siteList, String time) {
        int loseNum = 0;//null num
        for (int i = 0; i < siteList.size(); i++) {
            Site site = siteList.get(i);
            Map<String, String> map = site.getTimeValue();
            String value = map.get(time);
            site.setValue(value);
            if (value == null) {
                loseNum++;
            }
        }
        if (loseNum > 3) {
            return false;
        } else {
            return true;
        }
    }


    public static int[] getYjImg(String name) {
        int imgIndex = 0;
        int colorIndex = 0;
        int[] result = new int[2];
        switch (name) {
            case "暴雪橙色预警":
                imgIndex = 1;
                colorIndex = 1;
                break;
            case "暴雪红色预警":
                imgIndex = 2;
                colorIndex = 2;
                break;
            case "暴雪黄色预警":
                imgIndex = 3;
                colorIndex = 3;
                break;
            case "暴雪蓝色预警":
                imgIndex = 4;
                colorIndex = 4;
                break;
            case "暴雨橙色预警":
                imgIndex = 5;
                colorIndex = 1;
                break;
            case "暴雨红色预警":
                imgIndex = 6;
                colorIndex = 2;
                break;
            case "暴雨黄色预警":
                imgIndex = 7;
                colorIndex = 3;
                break;
            case "暴雨蓝色预警":
                imgIndex = 8;
                colorIndex = 4;
                break;
            case "冰雹橙色预警":
                imgIndex = 9;
                colorIndex = 1;
                break;
            case "冰雹红色预警":
                imgIndex = 10;
                colorIndex = 2;
                break;
            case "大风橙色预警":
                imgIndex = 11;
                colorIndex = 1;
                break;
            case "大风红色预警":
                imgIndex = 12;
                colorIndex = 2;
                break;
            case "大风黄色预警":
                imgIndex = 13;
                colorIndex = 3;
                break;
            case "大雾橙色预警":
                imgIndex = 14;
                colorIndex = 1;
                break;
            case "大雾红色预警":
                imgIndex = 15;
                colorIndex = 2;
                break;
            case "大雾黄色预警":
                imgIndex = 16;
                colorIndex = 3;
                break;
            case "道路结冰橙色预警":
                imgIndex = 17;
                colorIndex = 1;
                break;
            case "道路结冰红色预警":
                imgIndex = 18;
                colorIndex = 2;
                break;
            case "道路结冰黄色预警":
                imgIndex = 19;
                colorIndex = 3;
                break;
            case "干旱橙色预警":
                imgIndex = 20;
                colorIndex = 1;
                break;
            case "干旱红色预警":
                imgIndex = 21;
                colorIndex = 2;
                break;
            case "高温橙色预警":
                imgIndex = 22;
                colorIndex = 1;
                break;
            case "高温红色预警":
                imgIndex = 23;
                colorIndex = 2;
                break;
            case "寒潮橙色预警":
                imgIndex = 24;
                colorIndex = 1;
                break;
            case "寒潮红色预警":
                imgIndex = 25;
                colorIndex = 2;
                break;
            case "寒潮黄色预警":
                imgIndex = 26;
                colorIndex = 3;
                break;
            case "寒潮蓝色预警":
                imgIndex = 27;
                colorIndex = 4;
                break;
            case "雷电橙色预警":
                imgIndex = 28;
                colorIndex = 1;
                break;
            case "雷电红色预警":
                imgIndex = 29;
                colorIndex = 2;
                break;
            case "雷电黄色预警":
                imgIndex = 30;
                colorIndex = 3;
                break;
            case "霾橙色预警":
                imgIndex = 31;
                colorIndex = 1;
                break;
            case "霾黄色预警":
                imgIndex = 32;
                colorIndex = 3;
                break;
            case "霜冻橙色预警":
                imgIndex = 33;
                colorIndex = 1;
                break;
            case "霜冻黄色预警":
                imgIndex = 34;
                colorIndex = 3;
                break;
            case "霜冻蓝色预警":
                imgIndex = 35;
                colorIndex = 4;
                break;
            case "台风橙色预警":
                imgIndex = 36;
                colorIndex = 1;
                break;
            case "台风红色预警":
                imgIndex = 37;
                colorIndex = 2;
                break;
            case "台风黄色预警":
                imgIndex = 38;
                colorIndex = 3;
                break;
            case "台风蓝色预警":
                imgIndex = 39;
                colorIndex = 4;
                break;
        }
        result[0] = yjImg[imgIndex];
        result[1] = yjColor[colorIndex];
        return result;
    }

    /*
    除以10
     */
    public static String div10(String num) {
        String result = null;
        if (TextUtils.isEmpty(num) || num.equals(Constant.NO_DATA_FLAG)) {
            result = num;
        } else {
            DecimalFormat df = new DecimalFormat("0.0");
            double v = Double.parseDouble(num);
            v = v / 10;
            result = df.format(v);
        }
        return result;
    }

    /*
    除以100
     */
    public static String div100(String num) {
        String result = null;
        if (TextUtils.isEmpty(num) || num.equals(Constant.NO_DATA_FLAG)) {
            result = num;
        } else {
            DecimalFormat df = new DecimalFormat("0.0");
            double v = Double.parseDouble(num);
            v = v / 100;
            result = df.format(v);
        }
        return result;
    }


    /*
    保留一位小数
     */
    public static String saveOne(String sNum) {
        String result = null;
        if (TextUtils.isEmpty(sNum) || sNum.equals(Constant.NO_DATA_FLAG)) {
            result = sNum;
        } else {
            double num = Double.parseDouble(sNum);
            DecimalFormat df = new DecimalFormat("0.0");
            result = df.format(num);
        }
        return result;
    }

    /*
   格式化时间  //20170417110000
    */
    public static String getHourByTime(String time) {
        String result = Constant.NO_DATA_FLAG;
        if (time != null) {
            result = time.substring(8, 10);
        }
        return result;
    }

    /*
    格式化时间
     */
    public static String getToMinByTime(String time) {
        //20170417110000
        if (time != null) {
            String s = time.substring(4, 12);
            return s.substring(0, 2) + "月" +
                    s.substring(2, 4) + "日" +
                    s.substring(4, 6) + "时" +
                    s.substring(6, 8) + "分";
        } else {
            return null;
        }
    }

    public static String getToHourByTime(String time) {
        //20170417110000
        if (time != null) {
            String s = time.substring(4, 12);
            return s.substring(0, 2) + "月" +
                    s.substring(2, 4) + "日" +
                    s.substring(4, 6) + "时";
        } else {
            return null;
        }
    }

    public static String getToDayByTime(String time) {
        //20170417110000
        if (time != null) {
            String s = time.substring(4, 12);
            return s.substring(0, 2) + "月" +
                    s.substring(2, 4) + "日";
        } else {
            return null;
        }
    }

    /*
    格式化时间
   */
    public static String getNumDate(String date) {
        //20170417110000
        if (date != null) {
            String time = date.substring(8, 12);
            return time;
        } else {
            return null;
        }
    }

    /*
    格式化时间
    */
    public static float getFloatTime(String date) {
        float time;
        //20170417110000
        if (date != null && date.length() >= 12) {
            String d = date.substring(8, 12);
            time = Float.parseFloat(d);
        } else {
            time = 0;
        }
        return time;
    }

    public static String getXName(int i) {
        String name = null;
        switch (i) {
            case 0:
                name = "20:00";
                break;
            case 1:
                name = "21:00";
                break;
            case 2:
                name = "22:00";
                break;
            case 3:
                name = "23:00";
                break;
            case 4:
                name = "00:00";
                break;
            case 5:
                name = "01:00";
                break;
            case 6:
                name = "02:00";
                break;
            case 7:
                name = "03:00";
                break;
            case 8:
                name = "04:00";
                break;
            case 9:
                name = "05:00";
                break;
            case 10:
                name = "06:00";
                break;
            case 11:
                name = "07:00";
                break;
            case 12:
                name = "08:00";
                break;
            case 13:
                name = "09:00";
                break;
            case 14:
                name = "10:00";
                break;
            case 15:
                name = "11:00";
                break;
            case 16:
                name = "12:00";
                break;
            case 17:
                name = "13:00";
                break;
            case 18:
                name = "14:00";
                break;
            case 19:
                name = "15:00";
                break;
            case 20:
                name = "16:00";
                break;
            case 21:
                name = "17:00";
                break;
            case 22:
                name = "18:00";
                break;
            case 23:
                name = "19:00";
                break;
            case 24:
                name = "20:00";
                break;
        }
        return name;
    }

    public static String addTown(String siteName) {
        String result = siteName;
        switch (siteName) {
            case "前仓":
                result = "前仓(前仓)";
                break;
            case "柘岭下":
                result = "柘岭下(方岩)";
                break;
            case "桥下":
                result = "桥下(龙山)";
                break;
            case "李店":
                result = "李店(西城)";
                break;
            case "八字墙":
                result = "八字墙(花街)";
                break;
            case "象珠":
                result = "象珠(象珠)";
                break;
            case "杨溪":
                result = "杨溪(石柱)";
                break;
            case "上黄":
                result = "上黄(西城)";
                break;
            case "溪岸":
                result = "溪岸(芝英)";
                break;
            case "中山":
                result = "中山(唐先)";
                break;
            case "棠溪":
                result = "棠溪(西溪)";
                break;
            case "舟山":
                result = "舟山(舟山)";
                break;
            case "永祥":
                result = "永祥(江南)";
                break;
            case "新楼":
                result = "新楼(舟山)";
                break;
            case "长城":
                result = "长城(东城)";
                break;
            case "筻里":
                result = "筻里(石柱)";
                break;
            case "夏杜曹":
                result = "夏杜曹(唐先)";
                break;
            case "姚塘":
                result = "姚塘(石柱)";
                break;
            case "西竹园":
                result = "西竹园(东城)";
                break;
            case "外国语":
                result = "外国语(江南)";
                break;
            case "五峰":
                result = "五峰(方岩)";
                break;
            case "大寒山":
                result = "大寒山(花街)";
                break;
            case "尚仁":
                result = "尚仁(花街)";
                break;
            case "四中":
                result = "四中(江南)";
                break;
            case "弓塘":
                result = "弓塘(古山)";
                break;
            case "椒坑":
                result = "椒坑(象珠)";
                break;
            case "峰箬":
                result = "峰箬(石柱)";
                break;
            case "永康中学":
                result = "永康中学(东城)";
                break;
            case "西溪":
                result = "西溪(西溪)";
                break;
            case "永康":
                result = "永康";
                break;
        }
        return result;
    }

    /*
     把区域站站号取出来拼接成字符串
    */
    public static String getSiteIdsBySites(List<Site> siteList) {
        String ids = null;
        if (siteList != null && siteList.size() > 0) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < siteList.size(); i++) {
                sb.append(siteList.get(i).getStationId()).append(",");
            }
            String s = sb.toString();
            ids = s.substring(0, s.length() - 1);
        }
        return ids;
    }

    public static String getAfterDate(Date date) {
        String time = null;
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            time = sdf.format(date);
            time = time.substring(0, 8) + "200000";
        }
        return time;
    }

    private static void clearCt(Site site) {
        site.setValue(null);
    }


    /*
    保留分钟十位数字
    */
    public static String formatMin(String time) {
        String result = null;
        if (time != null) {
            result = time.substring(0, 11) + "000";
        }
        return result;
    }

    /*
    格式化 小时
    20170927131056->20170927130000
    */
    public static String formatHor(String time) {
        String result = time;
        try {
            result = time.substring(0, 10) + "0000";
        } catch (Exception e) {
            Log.e(TAG, "formatHor: " + e.getMessage());
        } finally {
            return result;
        }
    }

    /*
    保留十分钟
    */
    public static String saveTenMin(String time) {
        String result = null;
        if (time != null) {
            String newTime = time.substring(4, 12);
            result = newTime.substring(0, 2) + "月" +
                    newTime.substring(2, 4) + "日" +
                    newTime.substring(4, 6) + ":" +
                    newTime.substring(6, 7) + "0分";
        }
        return result;
    }

    /*
    格式化分钟
    */
    public static String saveMin(String time) {
        String result = null;
        if (time != null) {
            String newTime = time.substring(4, 12);
            result = newTime.substring(0, 2) + "月" +
                    newTime.substring(2, 4) + "日" +
                    newTime.substring(4, 6) + ":" +
                    newTime.substring(6, 8) + "";
        }
        return result;
    }

    /*
    格式化小时
    */
    public static String saveHour(String time) {
        String result = null;
        if (time != null && time.length() >= 10) {
            String newTime = time.substring(4, 10);
            result = newTime.substring(0, 2) + "月" +
                    newTime.substring(2, 4) + "日" +
                    newTime.substring(4, 6) + "时";
        }
        return result;
    }

    /*
     格式化天
     */
    public static String saveDay(String time) {
        String result = null;
        if (time != null) {
            String newTime = time.substring(4, 8);
            result = newTime.substring(0, 2) + "月" +
                    newTime.substring(2, 4) + "日";
        }
        return result;
    }

    // TODO: 2017/10/13 ---------------------------
    private void tryCatch() {
        try {

        } catch (Exception e) {
            Log.e(TAG, "parseFore: " + e.getMessage());
        }
    }

    public static String parseFore(String foreText, String foreType) {
        String replaceM = foreText.replace("-1", "m1").replace("-2", "m2").replace("-3", "m3").replace("-4", "m4").replace("-5", "m5").
                replace("-6", "m6").replace("-7", "m7").replace("-8", "m8").replace("-9", "m9");
        String replace = replaceM.replace("-", "").replace("m", "-");
        String result = replace;
        try {
            switch (foreType) {
                case Constant.Fore.SHORT_TIME:
                    result = ParseUtil.parseShortTime(replace);
                    break;
                case Constant.Fore.SHORT_DATE:
                    result = ParseUtil.parseShortDate(replace);
                    break;
                case Constant.Fore.A_WEEK:
                    result = ParseUtil.parseAWeek(replace);
                    break;
            }
        } catch (Exception e) {
            Log.e(TAG, "parseFore: " + e.getMessage());
        }
        return result;
    }

    /*
    解析短时预报
     */

    private static String parseShortTime(String foreText) {
        String result = foreText;
        try {
            String s = foreText.replace("预报员", "#").replace("。", "").replace(",", "");
            String[] split = s.split("#");
            if (split != null && split.length == 2) {
                result = split[0].replace("天气预报：", "\n" + "\n" + "天气预报：").
                        replace("风的预报：", "\n" + "\n" + "风的预报：").
                        replace("气温预报：", "\n" + "\n" + "气温预报：").
                        replace("永康市气象台", "发布时间：").
                        replace("发布的未来三小时天气预报", "");
            }
        } catch (Exception e) {
            Log.e(TAG, "parseShortTime: " + e.getMessage());
        }
        return result;
    }

    /*
    解析短期预报
     */
    private static String parseShortDate(String foreText) {
        String result = foreText;
        try {
            String s = foreText.replace("预报员", "#").replace("。", "").replace(",", "");
            String[] split = s.split("#");
            if (split != null) {
                result = split[0].replace("永康市气象台发布的天气预报", "").replace("天气预报：", "\n" + "\n" + "天气预报：").
                        replace("高温报告：", "\n" + "\n" + "高温报告：").
                        replace("风的预报：", "\n" + "\n" + "风的预报：").
                        replace("气温预报：", "\n" + "\n" + "气温预报：").
                        replace("酷热警报：", "\n" + "\n" + "酷热警报：").
                        replace("台风消息：", "\n" + "\n" + "台风消息：").
                        replace("森林火险气象等级预报：", "\n" + "\n" + "森林火险气象等级预报：").
                        replace("地质灾害气象等级预报：", "\n" + "\n" + "地质灾害气象等级预报：");
            }

        } catch (Exception e) {
            Log.e(TAG, "parseShortDate: " + e.getMessage());
        }
        return result;
    }

    /*
    解析一周预报
     */
    private static String parseAWeek(String foreText) {
        String result = foreText;
        StringBuilder sb = new StringBuilder();
        try {
            String s = foreText.replace("具体分日预报：", "#").replace("星期天", "周日").replace("预报员", "#").replace("星期", "周");
            String[] split = s.split("#");
            if (split != null && split.length == 3) {
                String title = split[0].replace("永康市气象台", "发布时间：").
                        replace("发布的一周趋势预报：", "").
                        replace("天气趋势预报：", "\n" + "\n" + "天气趋势预报：");
                String week = split[1].replace("级", "#");
                String[] weeks = week.split("#");
                for (int i = 0; i < weeks.length; i++) {
                    String day = weeks[i].substring(3, weeks[i].length() - 1).replace("，偏", "#").
                            replace("01", "1").replace("02", "2").replace("03", "3").replace("04", "4").replace("05", "5").
                            replace("06", "6").replace("07", "7").replace("08", "8").replace("09", "9");
                    String[] days = day.split("#");
                    sb.append(days[0] + "\n" + "\n");
                }
                result = title + "\n" + "\n" + sb.toString();
            }
        } catch (Exception e) {
            Log.e(TAG, "parseAWeek: " + e.getMessage());
        }
        return result;
    }

    /*
    根据定位后的乡镇获取其代表站
     */
    public static String getSiteIdAndTownByTownship(String township) {
        String id = Constant.YK_STA_ID;
        switch (township) {
            case "前仓":
                id = "K6019";//前仓
                break;
            case "方岩":
                id = "K6020";//柘岭下
                break;
            case "古山":
                id = "K6221";//弓塘
                break;
            case "西城":
                id = "K6028";//李店
                break;
            case "象珠":
                id = "K6030";//象珠
                break;
            case "石柱":
                id = "K6062";//杨溪
                break;
            case "芝英":
                id = "K6064";//溪岸
                break;
            case "唐先":
                id = "K6095";//中山
                break;
            case "西溪":
                id = "K6096";//棠溪
                break;
            case "舟山":
                id = "K6109";//舟山
                break;
            case "江南":
                id = "K6110";//永详
                break;
            case "东城":
                id = "K6192";//西竹园
                break;
            case "花街":
                id = "K6805";//尚仁
                break;
        }
        return id;
    }

    /*
    把字符串数组转化为集合
     */
    public static List<String> getListByArray(String[] array) {
        List<String> list = new ArrayList<>();
        if (array != null) {
            for (int i = 0; i < array.length; i++) {
                list.add(array[i]);
            }
        }
        return list;
    }

    public static Map<String, ArrayList<Site>> formatSite(ArrayList<Site> items) {

        Map<String, ArrayList<Site>> map = new HashMap<>();
        ArrayList<Site> ykItems = new ArrayList<Site>();
        ArrayList<Site> afterItems = new ArrayList<Site>();
        String[] array = Constant.ADD_SITEID_ARRAY;
        try {
            for (int i = 0; i < items.size(); i++) {
                Site item = items.get(i);
                //   Log.e(TAG, "sortList: " + item.getStationId() + " " + item.getStation_name());
                if (item.getStationId().equals(Constant.YK_STA_ID)) {
                    ykItems.add(item);
                }
                for (int j = 0; j < array.length; j++) {
                    if (item.getStationId().equals(array[j])) {
                        afterItems.add(item);
                        items.remove(item);
                        i--;
                        break;
                    }
                }
            }
            map.put("0", ykItems);
            map.put("1", items);
            map.put("2", afterItems);
            return map;
        } catch (Exception e) {
            Log.e(TAG, "exclude: " + e.getMessage());
            map.put("1", items);
            return map;
        }
    }

    public static List<Count> formatCount(List<Count> items) {
        List<Count> sortCounts = new ArrayList<>();
        String[] array = Constant.ADD_SITEID_ARRAY;
        try {
            for (int i = 7; i < items.size(); i++) {
                boolean flag = false;
                Count item = items.get(i);
                for (int j = 0; j < array.length; j++) {
                    if (item.getStationId().equals(array[j])) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    sortCounts.add(item);
                }
            }
            return sortCounts;
        } catch (Exception e) {
            Log.e(TAG, "exclude: " + e.getMessage());
            return null;
        }
    }


    /*
    统计页面排序操作
     */
    public static List<Count> getCountList(ArrayList<Site> siteList) {
        List<Count> counts = new ArrayList<>();
        Map<String, ArrayList<Site>> siteMap = formatSite(siteList);

        ArrayList<Site> ykSites = siteMap.get("0");
        ArrayList<Count> ykCounts = siteChCountSite(ykSites);

        ArrayList<Site> sortSites = siteMap.get("1");
        for (int i = 0; i < sortSites.size(); i++) {
            Site site = sortSites.get(i);
            if (site.getValue() == null || site.getValue().equals(Constant.NO_DATA_FLAG)) {
                site.setValue("-999");
            } else if (site.getValue().equals(Constant.NO_PRE_FLAG)) {
                site.setValue("0.0");
            }
        }
        ArrayList<Count> titleCounts = new ArrayList<>();
        try {
            double sum = 0;//求和
            int valid = 0;//有效值
            String max = Constant.NO_DATA_FLAG;//最大值
            String min = Constant.NO_DATA_FLAG;
            double avg = 0;//平均值
            sortList(sortSites);
            for (int i = 0; i < sortSites.size(); i++) {
                Site site = sortSites.get(i);
                if (i == 0) {
                    max = site.getValue();
                }
                if (!site.getValue().equals("-999")) {
                    sum += Double.parseDouble(site.getValue());
                    valid++;
                }
            }
            for (int i = sortSites.size() - 1; i >= 0; i--) {
                String s = sortSites.get(i).getValue();
                if (!s.equals("-999")) {
                    min = s;
                    break;
                }
            }
            if (valid != 0) {
                avg = sum / valid;
            }
            //0.标题
            Count countTitle = new Count();
            countTitle.setName("统计要素");
            countTitle.setValue("统计值");
            titleCounts.add(countTitle);
            //1.站点总数
            Count countSum = new Count();
            countSum.setName("站点总数");
            countSum.setValue(sortSites.size() + "");
            titleCounts.add(countSum);
            //2.缺测站数
            Count countUnValid = new Count();
            countUnValid.setName("缺测站数");
            countUnValid.setValue((sortSites.size() - valid) + "");
            titleCounts.add(countUnValid);
            //3.最大值
            Count countMax = new Count();
            countMax.setName("最大值");
            countMax.setValue(max);
            titleCounts.add(countMax);
            //4.最小值
            Count countMin = new Count();
            countMin.setName("最小值");
            countMin.setValue(min);
            titleCounts.add(countMin);
            //5.平均值
            Count countAvg = new Count();
            countAvg.setName("平均值");
            countAvg.setValue(ParseUtil.saveOne(avg + ""));
            titleCounts.add(countAvg);
            //5.隔板
            Count countName = new Count();
            countName.setName("站名");
            countName.setValue("观测值");
            titleCounts.add(countName);

            for (int i = 0; i < sortSites.size(); i++) {
                if (sortSites.get(i).getStationId().equals(Constant.YK_STA_ID)) {
                    sortSites.remove(i);
                    break;
                }
            }
            ArrayList<Count> sortCounts = siteChCountSite(sortSites);

            ArrayList<Site> unSortSites = siteMap.get("2");
            ArrayList<Count> unSortCounts = siteChCountSite(unSortSites);

            counts.addAll(titleCounts);
            counts.addAll(ykCounts);
            counts.addAll(sortCounts);
            counts.addAll(unSortCounts);

        } catch (Exception e) {
            Log.e(TAG, "getCountList: " + e.getMessage());
        }
        return counts;
    }

    private static ArrayList<Count> siteChCountSite(ArrayList<Site> sites) {
        ArrayList<Count> counts = new ArrayList<>();
        for (int i = 0; i < sites.size(); i++) {
            Site site = sites.get(i);
            Log.e(TAG, "siteChCountSite: " + site.getStationId() + ":" + site.getStation_name());
            Count count = new Count();
            count.setStationId(site.getStationId());
            count.setName(addTown(site.getStation_name()));
            count.setValue(site.getValue());
            counts.add(count);
        }
        return counts;
    }


    /*
    统计页面编辑后重新统计
     */
    public static void parseCountList(List<Count> countList, String siteType) {
        try {
            if (countList != null) {
                List<Count> sortCounts = formatCount(countList);
                double sum = 0;
                int valid = 0;
                String max = Constant.NO_DATA_FLAG;
                int startIndex = 7;
                double avg = 0;
                String min = Constant.NO_DATA_FLAG;
                if (sortCounts.size() > 0) {/*
                    Count count1 = countList.get(startIndex);
                    Count count2 = countList.get(startIndex + 1);
                    if (Double.parseDouble(count1.getValue()) < Double.parseDouble(count2.getValue())) {
                        max = count2.getValue();
                    } else {
                        max = count1.getValue();
                    }
                }
                if (countList.size() == 1) {
                    max = countList.get(startIndex).getValue();
                }*/
                    for (int i = 0; i < sortCounts.size(); i++) {
                        Count count = sortCounts.get(i);
                        if (i == 0) {
                            max = count.getValue();
                        }
                        if (!count.getValue().equals("-999")) {
                            sum += Double.parseDouble(count.getValue());
                            valid++;
                        }
                    }

                    for (int i = sortCounts.size() - 1; i > -1; i--) {
                        Count count = sortCounts.get(i);
                        if (!count.getValue().equals("-999")) {
                            min = count.getValue();
                            break;
                        }
                    }
                    if (valid != 0) {
                        avg = sum / valid;
                    }
                }
                countList.get(1).setValue(sortCounts.size()+ "");
                countList.get(2).setValue(sortCounts.size()- valid + "");
                countList.get(3).setValue(max);
                countList.get(4).setValue(min);
                countList.get(5).setValue(ParseUtil.saveOne(avg + ""));
            }
        } catch (
                Exception e)

        {
            Log.e(TAG, "getCountList: " + e.getMessage());
        }

    }

    /*
    根据详细三级菜单获得一级菜单
     */
    public static String getTypeByType(String type) {
        String result = Constant.Type.TEM;
        if (type.contains("tem")) {
            result = Constant.Type.TEM;
        } else if (type.contains("pre")) {
            result = Constant.Type.PRE;
        } else if (type.contains("wind")) {
            result = Constant.Type.WIND;
        }
        return result;
    }

    /*
     获取图片名字地址
    */
    public static String getPicNameUrl(String menuId) {
        String result = null;
        switch (menuId) {
            case "00":
                result = Constant.Url.GET_GRAD_RADAR_1H_FILE_NAME_URL;
                break;
            case "01":
                result = Constant.Url.GET_GRAD_RADAR_3H_FILE_NAME_URL;
                break;
            case "02":
                result = Constant.Url.GET_RAIN_RADAR_1H_FILE_NAME_URL;
                break;
            case "03":
                result = Constant.Url.GET_RAIN_RADAR_3H_FILE_NAME_URL;
                break;
            case "10":
                result = Constant.Url.GET_ZJ_RADAR_FILE_NAME_URL + "3";
                break;
            case "11":
                result = Constant.Url.GET_ZJ_RADAR_FILE_NAME_URL + "1";
                break;
            case "12":
                result = Constant.Url.GET_ZJ_RADAR_FILE_NAME_URL + "2";
                break;
            case "13":
                result = Constant.Url.GET_ZJ_RADAR_FILE_NAME_URL + "3";
                break;
            case "20":
                result = Constant.Url.GET_JH_RADAR_FILE_NAME_URL + "3";
                break;
            case "21":
                result = Constant.Url.GET_JH_RADAR_FILE_NAME_URL + "1";
                break;
            case "22":
                result = Constant.Url.GET_JH_RADAR_FILE_NAME_URL + "2";
                break;
            case "23":
                result = Constant.Url.GET_JH_RADAR_FILE_NAME_URL + "3";
                break;
        }
        return result;
    }

    /*
    获取目录
     */
    public static String getFileDir(String menuId) {
        String result = null;
        switch (menuId) {
            case "00":
                result = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ykqx/pic/grad1";
                break;
            case "01":
                result = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ykqx/pic/grad3";
                break;
            case "02":
                result = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ykqx/pic/rain1";
                break;
            case "03":
                result = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ykqx/pic/rain3";
                break;
            case "10":
            case "11":
            case "12":
            case "13":
                result = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ykqx/pic/zjRadar";
                break;
            case "20":
            case "21":
            case "22":
            case "23":
                result = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ykqx/pic/jhRadar";
                break;
        }
        if (result != null) {
            File file = new File(result);
            if (!file.exists()) {
                file.mkdirs();
            }
        }
        return result;
    }

    /*
    获取图片地址
     */
    public static String getPicUrl(String menuId, String picName) {
        String result = null;
        switch (menuId) {
            case "00":
                result = Constant.Url.GET_GRAD_RADAR_1H_URL + picName;
                break;
            case "01":
                result = Constant.Url.GET_GRAD_RADAR_3H_URL + picName;
                break;
            case "02":
                result = Constant.Url.GET_RAIN_RADAR_1H_URL + picName;
                break;
            case "03":
                result = Constant.Url.GET_RAIN_RADAR_3H_URL + picName;
                break;
            case "10":
                result = Constant.Url.GET_ZJ_RADAR_FILE_URL + picName;
                break;
            case "11":
                result = Constant.Url.GET_ZJ_RADAR_FILE_URL + picName;
                break;
            case "12":
                result = Constant.Url.GET_ZJ_RADAR_FILE_URL + picName;
                break;
            case "13":
                result = Constant.Url.GET_ZJ_RADAR_FILE_URL + picName;
                break;
            case "20":
                result = Constant.Url.GET_JH_RADAR_FILE_URL + picName;
                break;
            case "21":
                result = Constant.Url.GET_JH_RADAR_FILE_URL + picName;
                break;
            case "22":
                result = Constant.Url.GET_JH_RADAR_FILE_URL + picName;
                break;
            case "23":
                result = Constant.Url.GET_JH_RADAR_FILE_URL + picName;
                break;
        }
        return result;
    }

    public static String getRadarTitle(String menuId, String picName) {
        String result = picName;
        if (picName != null) {
            switch (menuId) {
                case "00":
                    result = picName.replace(".png", "") + "0000";
                    result = ParseUtil.saveHour(result) + " 1小时预测";
                    break;
                case "01":
                    result = picName.replace(".png", "") + "0000";
                    result = ParseUtil.saveHour(result) + " 3小时预测";
                    break;
                case "02":
                    result = picName.replace(".png", "") + "0000";
                    result = ParseUtil.saveHour(result) + " 1小时实况";
                    break;
                case "03":
                    result = picName.replace(".png", "") + "0000";
                    result = ParseUtil.saveHour(result) + " 3小时实况";
                    break;
                case "10":
                case "11":
                case "12":
                case "13":
                    result = picName.replace("MOSAICHREF000", "").replace("GIF", "").replace(".", "");
                    result = ParseUtil.saveMin(DateUtil.add8Hour(result));
                    break;
                case "20":
                case "21":
                case "22":
                case "23":
                    result = saveMin(picName.replace(".00.37.579.GIF", "").replace(".", ""));
                    break;
            }
        }
        return result;
    }

    public static void sortList(List<Site> sites) {
        for (int i = 0; i < sites.size() - 1; i++) {// 外层控制冒泡次数
            Site s = sites.get(i);
            //    Log.e(TAG, "sortList: " + s.getStationId() + " " + s.getStation_name());
            for (int j = 0; j < (sites.size() - 1 - i); j++) {//
                Site site1 = sites.get(j);
                Site site2 = sites.get(j + 1);
                if (Double.parseDouble(site1.getValue()) < Double.parseDouble(site2.getValue())) {
                    String id = site2.getStationId();
                    String name = site2.getStation_name();
                    String value = site2.getValue();

                    site2.setStationId(site1.getStationId());
                    site2.setStation_name(site1.getStation_name());
                    site2.setValue(site1.getValue());

                    site1.setStationId(id);
                    site1.setStation_name(name);
                    site1.setValue(value);
                }
            }
        }
    }
}



